Python实现遗传算法(二进制编码)求函数最优值

目标函数

maxf(x1,x2)=21.5+x1sin(4πx1)+x2sin(20πx2)
3.0x112.1
4.1x35.8

编码方式

本程序采用的是二进制编码精确到小数点后五位,经过计算可知对于 x1 其编码长度为18,对于 x2 其编码长度为15,因此每个基于的长度为33。

参数设置

种群大小 pop=100 ,交配池 Ppool=pop/2 ,变异概率 Pm=100 ,交叉概率 Pc=100 ,最大迭代次数 Gmax=300 ,自学习变异概率 Spm=0.05

算法步骤

设计的程序主要分为以下步骤:1、参数设置;2、种群初始化;3、用轮盘赌方法选择其中一半较好的个体作为父代;4、交叉和变异;5、更新最优解;6、对最有个体进行自学习操作;7结果输出。其算法流程图为:
流程图

算法结果

由程序输出可知其最终优化结果为38.85029, x1=11.625589x2=5.725031
输出基因编码为[1 1 0 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 1 1]。

代码

import numpy as np
import random
import math
import copy

class Ind():
    def __init__(self):
        self.fitness = 0
        self.x = np.zeros(33)
        self.place = 0
        self.x1 = 0
        self.x2 = 0

def Cal_fit(x, upper, lower):   #计算适应度值函数
    Temp1 = 0
    for i in range(18):
        Temp1 += x[i] * math.pow(2, i)
    Temp2 = 0
    for i in range(18, 33, 1):
        Temp2 += math.pow(2, i - 18) * x[i]
    x1 = lower[0] + Temp1 * (upper[0] - lower[0])/(math.pow(2, 18) - 1)
    x2 = lower[1] + Temp2 * (upper[1] - lower[1])/(math.pow(2, 15) - 1)
    if x1 > upper[0]:
        x1 = random.uniform(lower[0], upper[0])
    if x2 > upper[1]:
        x2 = random.uniform(lower[1], upper[1])
    return 21.5 + x1 * math.sin(4 * math.pi * (x1)) + x2 * math.sin(20 * math.pi * x2)
def Init(G, upper, lower, Pop):    #初始化函数
    for i in range(Pop):
        for j in range(33):
            G[i].x[j] = random.randint(0, 1)
        G[i].fitness = Cal_fit(G[i].x, upper, lower)
        G[i].place = i
def Find_Best(G, Pop):
    Temp = copy.deepcopy(G[0])
    for i in range(1, Pop, 1):
        if G[i].fitness > Temp.fitness:
            Temp = copy.deepcopy(G[i])
    return Temp

def Selection(G, Gparent, Pop, Ppool):    #选择函数
    fit_sum = np.zeros(Pop)
    fit_sum[0] = G[0].fitness
    for i in range(1, Pop, 1):
        fit_sum[i] = G[i].fitness + fit_sum[i - 1]
    fit_sum = fit_sum/fit_sum.max()
    for i in range(Ppool):
        rate = random.random()
        Gparent[i] = copy.deepcopy(G[np.where(fit_sum > rate)[0][0]])

def Cross_and_Mutation(Gparent, Gchild, Pc, Pm, upper, lower, Pop, Ppool):  #交叉和变异
    for i in range(Ppool):
        place = random.sample([_ for _ in range(Ppool)], 2)
        parent1 = copy.deepcopy(Gparent[place[0]])
        parent2 = copy.deepcopy(Gparent[place[1]])
        parent3 = copy.deepcopy(parent2)
        if random.random() < Pc:
            num = random.sample([_ for _ in range(1, 32, 1)], 2)
            num.sort()
            if random.random() < 0.5:
                for j in range(num[0], num[1], 1):
                    parent2.x[j] = parent1.x[j]
            else:
                for j in range(0, num[0], 1):
                    parent2.x[j] = parent1.x[j]
                for j in range(num[1], 33, 1):
                    parent2.x[j] = parent1.x[j]
            num = random.sample([_ for _ in range(1, 32, 1)], 2)
            num.sort()
            num.sort()
            if random.random() < 0.5:
                for j in range(num[0], num[1], 1):
                    parent1.x[j] = parent3.x[j]
            else:
                for j in range(0, num[0], 1):
                    parent1.x[j] = parent3.x[j]
                for j in range(num[1], 33, 1):
                    parent1.x[j] = parent3.x[j]
        for j in range(33):
            if random.random() < Pm:
                parent1.x[j] = (parent1.x[j] + 1) % 2
            if random.random() < Pm:
                parent2.x[j] = (parent2.x[j] + 1) % 2

        parent1.fitness = Cal_fit(parent1.x, upper, lower)
        parent2.fitness = Cal_fit(parent2.x, upper, lower)
        Gchild[2 * i] = copy.deepcopy(parent1)
        Gchild[2 * i + 1] = copy.deepcopy(parent2)

def Choose_next(G, Gchild, Gsum, Pop):    #选择下一代函数
    for i in range(Pop):
        Gsum[i] = copy.deepcopy(G[i])
        Gsum[2 * i + 1] = copy.deepcopy(Gchild[i])
    Gsum = sorted(Gsum, key = lambda x: x.fitness, reverse = True)
    for i in range(Pop):
        G[i] = copy.deepcopy(Gsum[i])
        G[i].place = i

def Decode(x):            #解码函数
    Temp1 = 0
    for i in range(18):
        Temp1 += x[i] * math.pow(2, i)
    Temp2 = 0
    for i in range(18, 33, 1):
        Temp2 += math.pow(2, i - 18) * x[i]
    x1 = lower[0] + Temp1 * (upper[0] - lower[0]) / (math.pow(2, 18) - 1)
    x2 = lower[1] + Temp2 * (upper[1] - lower[1]) / (math.pow(2, 15) - 1)
    if x1 > upper[0]:
        x1 = random.uniform(lower[0], upper[0])
    if x2 > upper[1]:
        x2 = random.uniform(lower[1], upper[1])
    return x1, x2

def Self_Learn(Best, upper, lower, sPm, sLearn):  #自学习操作
    num = 0
    Temp = copy.deepcopy(Best)
    while True:
        num += 1
        for j in range(33):
            if random.random() < sPm:
                Temp.x[j] = (Temp.x[j] + 1)%2
        Temp.fitness = Cal_fit(Temp.x, upper, lower)
        if Temp.fitness > Best.fitness:
            Best = copy.deepcopy(Temp)
            num = 0
        if num > sLearn:
            break
    return Best

if __name__ == '__main__':
    upper = [12.1, 5.8]
    lower = [-3, 4.1]
    Pop = 100
    Ppool = 50
    G_max = 300
    Pc = 0.8
    Pm = 0.1
    sPm = 0.05
    sLearn = 20
    G = np.array([Ind() for _ in range(Pop)])
    Gparent = np.array([Ind() for _  in range(Ppool)])
    Gchild = np.array([Ind() for _ in range(Pop)])
    Gsum = np.array([Ind() for _ in range(Pop * 2)])
    Init(G, upper, lower, Pop)       #初始化
    Best = Find_Best(G, Pop)
    for k in range(G_max):
        Selection(G, Gparent, Pop, Ppool)       #使用轮盘赌方法选择其中50%为父代
        Cross_and_Mutation(Gparent, Gchild, Pc, Pm, upper, lower, Pop, Ppool)  #交叉和变异生成子代
        Choose_next(G, Gchild, Gsum, Pop)       #选择出父代和子代中较优秀的个体
        Cbest = Find_Best(G, Pop)
        if Best.fitness < Cbest.fitness:
            Best = copy.deepcopy(Cbest)        #跟新最优解
        else:
            G[Cbest.place] = copy.deepcopy(Best)
        Best = Self_Learn(Best, upper, lower, sPm, sLearn)
        print(Best.fitness)
    x1, x2 = Decode(Best.x)
    print(Best.x)
    print([x1, x2])
  • 2
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用遗传算法函数最大的示例代码(使用Python语言): ```python import random # 定义目标函数 def f(x): return x**2 # 遗传算法参数设置 POPULATION_SIZE = 100 # 种群大小 CHROMOSOME_LENGTH = 8 # 染色体长度(二进制编码) CROSSOVER_RATE = 0.8 # 交叉概率 MUTATION_RATE = 0.01 # 变异概率 GENERATIONS = 50 # 迭代次数 # 初始化种群 def create_population(): population = [] for _ in range(POPULATION_SIZE): chromosome = ''.join(random.choices(['0', '1'], k=CHROMOSOME_LENGTH)) population.append(chromosome) return population # 计算染色体对应的函数 def calculate_fitness(chromosome): x = int(chromosome, 2) return f(x) # 选择操作(轮盘赌选择) def selection(population): fitness_values = [calculate_fitness(chromosome) for chromosome in population] total_fitness = sum(fitness_values) probabilities = [fitness / total_fitness for fitness in fitness_values] selected_indices = random.choices(range(POPULATION_SIZE), probabilities, k=POPULATION_SIZE) selected_population = [population[i] for i in selected_indices] return selected_population # 交叉操作(单点交叉) def crossover(parent1, parent2): if random.random() < CROSSOVER_RATE: crossover_point = random.randint(1, CHROMOSOME_LENGTH - 1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] return child1, child2 else: return parent1, parent2 # 变异操作(单点变异) def mutation(chromosome): mutated_chromosome = list(chromosome) for i in range(CHROMOSOME_LENGTH): if random.random() < MUTATION_RATE: mutated_chromosome[i] = '0' if chromosome[i] == '1' else '1' return ''.join(mutated_chromosome) # 遗传算法主过程 def genetic_algorithm(): population = create_population() for _ in range(GENERATIONS): new_population = [] # 选择 selected_population = selection(population) # 交叉和变异 for i in range(0, POPULATION_SIZE, 2): parent1 = selected_population[i] parent2 = selected_population[i + 1] child1, child2 = crossover(parent1, parent2) child1 = mutation(child1) child2 = mutation(child2) new_population.extend([child1, child2]) population = new_population # 寻找最解 best_chromosome = max(population, key=lambda chromosome: calculate_fitness(chromosome)) best_x = int(best_chromosome, 2) best_fitness = f(best_x) return best_x, best_fitness # 运行遗传算法 best_x, best_fitness = genetic_algorithm() print("最解 x =", best_x) print("最大 f(x) =", best_fitness) ``` 上述代码中使用了简单的二进制编码表示染色体,轮盘赌选择、单点交叉和单点变异作为遗传算法的基本操作。通过迭代多次,最终找到函数的最大。你可以根据自己的需进行参数调整和算法改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值